﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;
using System.Data;

namespace KZ.Lottery
{
    public partial class FrmReport : Form
    {
        public FrmReport()
        {
            InitializeComponent();
            dtpFrom.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
            dtpTo.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = DateTime.Today.GetFirstDayOfMonth();
            dtpTo.Value = DateTime.Today.GetLastDayOfMonth();

            LoadRule();
        }

        private void LoadRule()
        {
            try
            {
                RuleList = RuleBLL.GetRuleList();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRule", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private DataTable LoadReportByRuleChannel(DateTime? from, DateTime? to, string ruleID)
        {
            DataTable dt = new DataTable();

            bool isCon = true;
            string tableName;
            while (isCon)
            {
                DateTime toTemp;
                if (from.Value.Month <= 4)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_01_04";
                    toTemp = new DateTime(from.Value.Year, 04, 30);
                }
                else if (from.Value.Month <= 8)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_05_08";
                    toTemp = new DateTime(from.Value.Year, 08, 31);
                }
                else
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_09_12";
                    toTemp = new DateTime(from.Value.Year, 12, 31);
                }

                if (toTemp >= to)
                {
                    isCon = false;
                    toTemp = to.Value;
                }

                dt.Merge(RuleDetailGroupReportBLL.GetRuleDetailGroupReportListForRuleChannel(ruleID, from.Value, toTemp, tableName));

                from = toTemp.AddDays(1);
            }

            return dt;
        }

        private DataTable LoadReportByChannel(DateTime? from, DateTime? to, string ruleID)
        {
            DataTable dt = new DataTable();

            bool isCon = true;
            string tableName;
            while (isCon)
            {
                DateTime toTemp;
                if (from.Value.Month <= 4)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_01_04";
                    toTemp = new DateTime(from.Value.Year, 04, 30);
                }
                else if (from.Value.Month <= 8)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_05_08";
                    toTemp = new DateTime(from.Value.Year, 08, 31);
                }
                else
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_09_12";
                    toTemp = new DateTime(from.Value.Year, 12, 31);
                }

                if (toTemp >= to)
                {
                    isCon = false;
                    toTemp = to.Value;
                }

                dt.Merge(RuleDetailGroupReportBLL.GetRuleDetailGroupReportListForChannel(ruleID, from.Value, toTemp, tableName));

                from = toTemp.AddDays(1);
            }

            return dt;
        }

        private DataTable LoadReportByChannelTotal(DateTime? from, DateTime? to, string ruleID)
        {
            DataTable dt = new DataTable();

            bool isCon = true;
            string tableName;
            while (isCon)
            {
                DateTime toTemp;
                if (from.Value.Month <= 4)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_01_04";
                    toTemp = new DateTime(from.Value.Year, 04, 30);
                }
                else if (from.Value.Month <= 8)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_05_08";
                    toTemp = new DateTime(from.Value.Year, 08, 31);
                }
                else
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_09_12";
                    toTemp = new DateTime(from.Value.Year, 12, 31);
                }

                if (toTemp >= to)
                {
                    isCon = false;
                    toTemp = to.Value;
                }

                dt.Merge(RuleDetailGroupReportBLL.GetRuleDetailGroupReportListForChannelTotal(ruleID, from.Value, toTemp, tableName));

                from = toTemp.AddDays(1);
            }

            return dt;
        }

        private DataTable LoadReportByDate(DateTime? from, DateTime? to, string ruleID)
        {
            DataTable dt = new DataTable();

            bool isCon = true;
            string tableName;
            while (isCon)
            {
                DateTime toTemp;
                if (from.Value.Month <= 4)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_01_04";
                    toTemp = new DateTime(from.Value.Year, 04, 30);
                }
                else if (from.Value.Month <= 8)
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_05_08";
                    toTemp = new DateTime(from.Value.Year, 08, 31);
                }
                else
                {
                    tableName = "RuleDetailGroupReports_" + from.Value.Year + "_09_12";
                    toTemp = new DateTime(from.Value.Year, 12, 31);
                }

                if (toTemp >= to)
                {
                    isCon = false;
                    toTemp = to.Value;
                }

                dt.Merge(RuleDetailGroupReportBLL.GetRuleDetailGroupReportListForDate(ruleID, from.Value, toTemp, tableName));

                from = toTemp.AddDays(1);
            }

            return dt;
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                dtgRule.EndEdit();
                string id = Global.GetDtgValue(dtgRule, colRuleID.Index, colChk.Index);

                DateTime? from = dtpFrom.Value.Date;
                DateTime? to = dtpTo.Value.Date;
                if (chkFromStart.Checked)
                {
                    RuleDetailGroupReportBLL.GetFirstDateLastDate(out from, out to);
                }

                DataTable dt = new DataTable();
                switch (ReportType)
                {
                    case "ReportByRuleChannel":
                        //dt = LoadReportByRuleChannel(from, to, id);
                        dt = ReportBLL.GetReportForRuleChannel(id, from, to);
                        break;
                    case "ReportByChannel":
                        //dt = LoadReportByChannel(from, to, id);
                        dt = ReportBLL.GetReportForChannel(id, from, to);
                        break;
                    case "ReportByChannelTotal":
                        //dt = LoadReportByChannelTotal(from, to, id);
                        dt = ReportBLL.GetReportForRuleChannelTotal(id, from, to);
                        break;
                    case "ReportByDate":
                        //dt = LoadReportByDate(from, to, id);
                        dt = ReportBLL.GetReportForDate(id, from, to);
                        break;
                }

                if (dt.Rows.Count > 0)
                {
                    FrmPreview frm = new FrmPreview();
                    frm.ReportType = ReportType;
                    frm.DT = dt;
                    frm.From = dtpFrom.Value;
                    frm.To = dtpTo.Value;
                    frm.FromStart = chkFromStart.Checked;

                    this.Close();

                    frm.Show();
                }
                else
                {
                    Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_REPORT_NODATA, false);
                }
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
        }

        public string ReportType { get; set; }

        public List<Entities.Rule> RuleList
        {
            set
            {
                if (value.Count > 0)
                {
                    dtgRule.Rows.Add(value.Count);
                    for (int i = 0; i < value.Count; i++)
                    {
                        dtgRule[colRuleID.Index, i].Value = value[i].RuleID;
                        dtgRule[colRuleName.Index, i].Value = value[i].RuleName;
                        dtgRule[colDescription.Index, i].Value = value[i].Description;
                    }
                }
            }
        }
    }
}